﻿Public Class CreateFile



    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim path As String = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\Gadget Creator\tmp\"
        If TextBox1.Text = "" Then
            MsgBox("Необходимо указать имя файла", 64, "Ошибка")
        ElseIf ComboBox1.SelectedItem = Nothing Then
            MsgBox("Необходимо указать тип файла", 64, "Ошибка")
        ElseIf Not TextBox1.Text = "" And Not ComboBox1.SelectedItem = Nothing Then
            ' Узнаём тип файла
            Dim type As String = ""
            If ComboBox1.SelectedItem = "Веб-страница(*.html)" Then
                type = ".html"
            ElseIf ComboBox1.SelectedItem = "Java-скрипт(*.js)" Then
                type = ".js"
            ElseIf ComboBox1.SelectedItem = "Каскадная таблица стиля(*.css)" Then
                type = ".css"
            ElseIf ComboBox1.SelectedItem = "Текстовый файл(*.txt)" Then
                type = ".txt"
            End If
            If System.IO.File.Exists(path + TextBox1.Text + type) = True Then
                MsgBox("Файл с таким именем уже существует!", 48, "Ошибка")
            Else
                Try
                    Dim writer As New System.IO.StreamWriter(path + TextBox1.Text + type)
                    If type = ".html" Then
                        writer.Write(My.Settings.htmlsyntax)
                    Else
                        writer.Write("")
                    End If
                    writer.Close()
                    Dim editor As New TabPage
                    editor.Text = TextBox1.Text + type
                    editor.Name = TextBox1.Text + type
                    Dim htmlbox As New FastColoredTextBoxNS.FastColoredTextBox
                    htmlbox.Language = FastColoredTextBoxNS.Language.HTML
                    Dim reader As New System.IO.StreamReader(path + TextBox1.Text + type, System.Text.Encoding.GetEncoding(1251))
                    htmlbox.Text = reader.ReadToEnd
                    reader.Close()
                    htmlbox.Dock = DockStyle.Fill
                    editor.Controls.Add(htmlbox)
                    GeneralForm.tabcontrol.TabPages.Add(editor)
                    GeneralForm.tabcontrol.SelectedTab = editor
                    GeneralForm.refreshfiles()
                    Me.Close()
                Catch ex As Exception

                End Try
            End If
        End If
    End Sub
End Class